﻿using TorrentService;
using System.Collections.Generic;
using Meebey.SmartIrc4net;
using System;
using System.Reflection;
using System.ComponentModel;
using System.Threading;
using System.IO;
using BotTorrent;
using XmlSettings;

public class IrcMethods
{
    // Methods
    public List<string> EXIT(object eventargs, ircbot oIrcBot, string[] command)
    {
        List<string> retVal = new List<string>();
        IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
        Environment.Exit(1);
        return retVal;
    }

    public List<string> GETSET(object eventargs, ircbot oIrcBot, string[] command)
    {
        string settingname;
        object settingvalue;
        Exception e;
        List<string> retVal = new List<string>();
        IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
        switch (command.Length)
        {
            case 1:
                retVal.Add("Usage: !GETSET [Settingname] [Optional new value] . For the list of available variables issue !LISTCONFIG");
                return retVal;

            case 2:
                try
                {
                    settingname = command[1];
                    settingvalue = oIrcBot.Settings.GetType().GetProperty(settingname).GetValue(oIrcBot.Settings, null);
                    retVal.Add(string.Format("Value: [{0}] currently set to: {1}", settingname, settingvalue.ToString()));
                }
                catch (Exception exception1)
                {
                    e = exception1;
                    retVal.Add("Invalid setting name provided. Settings are case sensitive. For a list of settings issue !LISTCONFIG. Error was: " + e.Message);
                }
                return retVal;

            case 3:
                try
                {
                    settingname = command[1];
                    PropertyInfo propInfo = oIrcBot.Settings.GetType().GetProperty(settingname);
                    settingvalue = propInfo.GetValue(oIrcBot.Settings, null);
                    TypeConverter tc = TypeDescriptor.GetConverter(propInfo.PropertyType);
                    string oldsettingvalue = tc.ConvertToString(settingvalue);
                    object val = tc.ConvertFromString(command[2]);
                    propInfo.SetValue(oIrcBot.Settings, val, null);
                    retVal.Add(string.Format("Setting: {0} Was: {1} Now: {2}", settingname, oldsettingvalue, command[2]));
                }
                catch (Exception exception2)
                {
                    e = exception2;
                    retVal.Add("Invalid setting name provided. Settings are case sensitive. For a list of settings issue !LISTCONFIG. Error was: " + e.Message);
                }
                return retVal;
        }
        return retVal;
    }

    public List<string> LISTCONFIG(object eventargs, ircbot oIrcBot, string[] command)
    {
        List<string> retVal = new List<string>();
        try
        {
            IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
            PropertyInfo[] propInfo = oIrcBot.Settings.GetType().GetProperties();
            retVal.Add("Settings: ");
            foreach (PropertyInfo pi in propInfo)
            {
                Type propertyType = pi.PropertyType;
                object settingvalue = pi.GetValue(oIrcBot.Settings, null);
                TypeConverter tc = TypeDescriptor.GetConverter(propertyType);
                retVal.Add(pi.Name + " = " + settingvalue.ToString());
            }
        }
        catch (Exception)
        {
            retVal.Add("Unable to complete request");
        }
        return retVal;
    }

    public List<string> RSS(object eventargs, ircbot oIrcBot, string[] command)
    {
        Exception e;
        string help = "RSS USAGE: !RSS LIST, !RSS ADD, !RSS REMOVE, !RSS GETNOW, !RSS CLEARHISTORY";
        List<string> retVal = new List<string>();
        IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
        if (command.Length < 1)
        {
            retVal.Add(help);
            return retVal;
        }
        switch (command[1].ToUpper())
        {
            case "LIST":
            {
                int count = 0;
                foreach (RssFeed rssfeed in oIrcBot.Settings.RssFeeds)
                {
                    retVal.Add(count.ToString() + ")" + rssfeed.Url + " with filter " + rssfeed.Filter);
                    count++;
                }
                return retVal;
            }
            case "ADD":
            {
                RssFeed newrssfeed = new RssFeed();
                if (command.Length == 4)
                {
                    newrssfeed.Url = command[2];
                    newrssfeed.Filter = command[3];
                }
                else
                {
                    newrssfeed.Url = command[2];
                    newrssfeed.Filter = "";
                }
                oIrcBot.Settings.RssFeeds.Add(newrssfeed);
                oIrcBot.RssFeedManager.DownloadFeeds();
                this.SAVECONFIG(eventargs, oIrcBot, command);
                retVal.Add("Added feed and saved config");
                return retVal;
            }
            case "REMOVE":
                try
                {
                    oIrcBot.Settings.RssFeeds.RemoveAt(int.Parse(command[2]));
                    this.SAVECONFIG(eventargs, oIrcBot, command);
                    retVal.Add("Removed feed and saved config");
                }
                catch (Exception exception1)
                {
                    e = exception1;
                    retVal.Add(e.Message);
                    retVal.Add(help);
                }
                return retVal;

            case "GETNOW":
                oIrcBot.SendMessage(SendType.Message, oIrcBot.Settings.IrcSettings.ircChannel, "Checking Rss Feeds.....");
                new Thread(new ThreadStart(oIrcBot.RssFeedManager.DownloadFeeds)).Start();
                return retVal;

            case "CLEARHISTORY":
                try
                {
                    File.Delete("rsshistory.dat");
                    retVal.Add("Rss history file wiped");
                    return retVal;
                }
                catch (Exception exception2)
                {
                    e = exception2;
                    retVal.Add("Failed to wipe rss history file: " + e.Message);
                    return retVal;
                }
                break;
        }
        retVal.Add("Syntax error");
        return retVal;
    }

    public List<string> SAVECONFIG(object eventargs, ircbot oIrcBot, string[] command)
    {
        List<string> retVal = new List<string>();
        IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
        try
        {
            oIrcBot.Settings.Save(oIrcBot.Settings.ConfigPath);
            retVal.Add("Successfully saved config to " + oIrcBot.Settings.ConfigPath);
        }
        catch (Exception)
        {
            retVal.Add("Unable to save config to " + oIrcBot.Settings.ConfigPath);
        }
        return retVal;
    }

    public List<string> TORRENT(object eventargs, ircbot oIrcBot, string[] command)
    {
        List<string> retVal = new List<string>();
        IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
        string help = "Torrent commands: !TORRENT STOP <ID>, !TORRENT START <ID>, !TORRENT REMOVE <ID> <YES|NO>";
        if (command.Length < 2)
        {
            retVal.Add(help);
            return retVal;
        }
        string torrentAction = command[1].ToUpper();
        if (torrentAction == null)
        {
            return retVal;
        }
        if (!(torrentAction == "STOP"))
        {
            if (torrentAction == "START")
            {
                return PrivateIrcMethods.ircStartTorrent(command, oIrcBot.BTClient);
            }
            if (torrentAction == "LOAD")
            {
                return PrivateIrcMethods.ircLoadTorrent(command, oIrcBot);
            }
            return retVal;
        }
        return PrivateIrcMethods.ircStopTorrent(command, oIrcBot.BTClient);
    }

    public List<string> TORRENTSTATUS(object eventargs, ircbot oIrcBot, string[] command)
    {
        List<string> retVal = new List<string>();
        IrcEventArgs oEventArgs = (IrcEventArgs) eventargs;
        if (oIrcBot.BTClient.TorrentList.Count == 0)
        {
            retVal.Add("No torrents currently loaded.");
        }
        for (int i = 0; i < oIrcBot.BTClient.TorrentList.Count; i++)
        {
            try
            {
                try
                {
                    string torrentinfo = null;
                    torrentinfo = ((torrentinfo + string.Format("\x0002{0}\x0002)", i.ToString()) + string.Format(" \x0002Name\x0002[{0}]", oIrcBot.BTClient.TorrentList[i].Torrent.Name)) + string.Format(" \x0002Progress\x0002[{0}]", oIrcBot.BTClient.TorrentList[i].Progress) + string.Format(" \x0002DL\x0002[{0}K/s]", oIrcBot.BTClient.TorrentList[i].Monitor.DownloadSpeed / 0x400)) + string.Format("\x0002State\x0002[{0}]", oIrcBot.BTClient.TorrentList[i].State) + string.Format("\x0002S/L\x0002[{0}/{1}]", oIrcBot.BTClient.TorrentList[i].Peers.Seeds.ToString(), oIrcBot.BTClient.TorrentList[i].Peers.Leechs.ToString());
                    retVal.Add(torrentinfo);
                }
                catch (Exception)
                {
                    retVal.Add("There was an error getting torrents status");
                }
            }
            finally
            {
            }
        }
        return retVal;
    }

    public List<string> UPTIME(object eventargs, ircbot oIrcBot, string[] command)
    {
        List<string> retVal = new List<string>();
        TimeSpan ts = PrivateIrcMethods.GetUptime();
        string uptime = "System Uptime: " + ts.Hours.ToString() + " Hours, " + ts.Minutes.ToString() + " Minutes, " + ts.Seconds.ToString() + " Seconds.";
        retVal.Add(uptime);
        return retVal;
    }
}

 

 
